package httpHandler

import (
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	"gitee.com/zaiqiang231/go-base-app/base_app/long_link"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/config"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/http/service"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model/req"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CommentCreate(c *gin.Context) {
	var request req.CreateCommentRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	result, err := service.ServiceCommentInstance.CommentCreate(c, request, uid)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	long_link.PushMessage(config.GlobalAppConfig.PushAddr, "1000", strconv.FormatUint(request.PostUID, 10), "10", "您收到新的评论！")

	baseHttp.WriteResponse(c, 0, "success", result)
}

func CommentReply(c *gin.Context) {
	var request req.CommentReplyRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	result, err := service.ServiceCommentInstance.CommentReply(c, request, uid)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", result)
}

func CommentPage(c *gin.Context) {
	var request req.CommentPageListRequest
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, cursor, hasMore, err := service.ServiceCommentInstance.CommentPage(c, request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"comments": list,
		"has_more": hasMore,
		"cursor":   cursor,
	})
}

func CommentLike(c *gin.Context) {
	var request req.CommentLikeRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	err := service.ServiceCommentInstance.CommentLike(c, uid, request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", nil)
}

func CommentDelete(c *gin.Context) {
	var request req.CommentDeleteRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceCommentInstance.CommentDelete(c, request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", nil)
}

func CommentFind(c *gin.Context) {
	var request req.CommentFindRequest
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	data, err := service.ServiceCommentInstance.GetReplyComment(c, request.CommentID, int(request.Page))
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", data)
}
